package com.jic.manager.controller.manage.sys;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.member.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.manager.model.vo.request.SysProtocolRequest;
import com.jic.manager.model.vo.response.SysProtocolResponse;
import com.jic.manager.model.entity.SysProtocol;
import com.jic.manager.service.SysProtocolService;

 @RestController
 @Slf4j
 @RequestMapping("/sysProtocol")
 @Api(tags = " [协议管理]")
public class SysProtocolController {
	@Autowired
	private SysProtocolService service;
	
	
     /**
      *新增记录
      */
    @ApiOperation(value = "新增[协议管理]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody SysProtocolRequest request) throws Throwable {
     		SysProtocol sysProtocol = new SysProtocol();
    		BeanUtils.copyProperties(request, sysProtocol);
    	    long flag = service.save(sysProtocol);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      */
     @ApiOperation(value = "保存或更新[协议管理]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody SysProtocolRequest request) throws Throwable {
      		SysProtocol sysProtocol = new SysProtocol();
		     BeanUtils.copyProperties(request, sysProtocol);
	         long flag =   service.saveOrUpdate(sysProtocol);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      */
     @ApiOperation(value = "删除[协议管理]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody SysProtocolRequest request) throws Throwable {
      		SysProtocol sysProtocol = new SysProtocol();
	     	BeanUtils.copyProperties(request, sysProtocol);
	         long flag = service.deleteByPrimaryKeyLogically(sysProtocol);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      */
     @ApiOperation(value = "修改[协议管理]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody SysProtocolRequest request) throws Throwable {
      		SysProtocol sysProtocol = new SysProtocol();
	     	BeanUtils.copyProperties(request, sysProtocol);
	         long flag = service.updateByPrimaryKeySelective(sysProtocol);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[协议管理]详情")
      @ResponseBody
     public RestResult<SysProtocolResponse> getByPrimaryKey(@Validated  @RequestBody SysProtocolRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	SysProtocol sysProtocol = new SysProtocol();
	     	BeanUtils.copyProperties(request, sysProtocol);
	     	SysProtocol result = service.getByPrimaryKey(sysProtocol);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     SysProtocolResponse bo = new SysProtocolResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new SysProtocolResponse());
     }
	
    /**
      * 分页查询
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<SysProtocol>> listPage (@RequestBody SysProtocolRequest request) throws Throwable {
      SysProtocol param = new SysProtocol();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *SYS_PROTOCOL 查询列表（不分页）
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [协议管理] ")
      @ResponseBody
      public RestResult<List<SysProtocolResponse>>  listSysProtocolAllRecord(@RequestBody SysProtocolRequest request) throws Throwable {
      		SysProtocol param = new SysProtocol();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<SysProtocol> list = service.listAllRecord(param);
			List<SysProtocolResponse> responseList = list.stream().map(temp -> {
			SysProtocolResponse bo = new SysProtocolResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

        /**
         * 启用
         * @return
         */
      @PostMapping("/updatestartUsing")
      @ApiOperation(value = "启用")
      @ResponseBody
      public RestResult updatestartUsing(@RequestBody SysProtocolRequest request) throws Throwable {
          RestResult restResult = new RestResult();
      		SysProtocol param = new SysProtocol();
	     	BeanUtils.copyProperties(request, param);
            Integer ger = service.updatestartUsing(param);
            if (ger == 1){
                return RestResult.success(service.updateAll(request.getId()));
            }
          return RestResult.success(1);
      }

   /**
         * 启用
       */
      @PostMapping("/updateblockUp")
      @ApiOperation(value = "停用")
      @ResponseBody
      public RestResult<Integer>  updateblockUp(@RequestBody SysProtocolRequest request) throws Throwable {
      		SysProtocol param = new SysProtocol();
	     	BeanUtils.copyProperties(request, param);

		    return RestResult.success(service.updateblockUp(param));
      }




     }

	
